<?php

class APPCore_Form_Element_Ckeditor extends Zend_Form_Element_Textarea {

    /**
     * Array config for ckeditor
     * @var array
     */
    private $_config = array(
        'toolbar' => 'MyToolbar',
        'directory_upload' => '/upload/ck'
    );

     /**
     * Use formTextarea view helper by default
     * @var string
     */
    public $helper = 'formCkeditor';

    /**
     * Set base url from http
     * @var string
     */
    private $_baseUrl =  '';

    /**
     * Construct for ckeditor
     * @param type $spec
     * @param type $options
     */
    public function __construct($spec, $options = null) {
        parent::__construct($spec, $options);

        // if http base url is empty the system get REQUEST URI
        if(empty ($this->_baseUrl)){
            $this->_baseUrl = $_SERVER['REQUEST_URI'];
        }

        //grab a reference to the view rendering the form element
        $view = $this->getView();

        //include scripts and initialize the ckeditor and ckfinder
//        $view->headScript()->appendFile(
//                $this->_baseUrl.'/scripts/ckeditor/ckeditor.js', 'text/javascript'
//        );
//        $view->headScript()->appendFile(
//                $this->_baseUrl.'/scripts/ckfinder/ckfinder.js', 'text/javascript'
//        );
//        $view->headScript()->appendFile(
//                $this->_baseUrl.'/scripts/ckeditor/adapters/jquery.js', 'text/javascript'
//        );

        //give the textarea a class name that ckeditor recognises
        //$this->setAttrib('class', 'ckeditor');

        //set options
        $this->setAttrib('config', $this->_config);
    }

    /**
     * set width for ckedior
     * @param int $width
     */
    public function setWidth($width){
        if(!empty ($width)){
            $this->_config['width'] = $width;
            $this->setAttrib('config', $this->_config);
        }
    }

    /**
     * set width for ckeditor
     * @param int $height
     */
    public function setHeight($height){
        if(!empty ($height)){
            $this->_config['height'] = $height;
            $this->setAttrib('config', $this->_config);
        }
    }

    /**
     * set toolbar for ckeditor
     * @param string $nameToolbar
     */
    public function setToolbar($nameToolbar){
        if(!empty ($nameToolbar)){
            $this->_config['toolbar'] = $nameToolbar;
            $this->setAttrib('config', $this->_config);
        }
    }

    /**
     * set directory upload file for ckeditor
     * @param string $directoryUpload
     */
    public function setDirectoryUpload($directoryUpload){
        if(!empty ($directoryUpload)){
            $this->_config['directoryUpload'] = $directoryUpload;
            $this->setAttrib('config', $this->_config);
        }
    }
}